package com.thirteenClassicsServer.cacheService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.thirteenClassics.constant.MessageConstant;
import com.thirteenClassics.dto.UserLoginDTO;
import com.thirteenClassics.entity.User;
import com.thirteenClassics.exception.BaseException;
import com.thirteenClassics.exception.PasswordErrorException;
import com.thirteenClassicsServer.mapper.UserMapper;
import com.thirteenClassicsServer.service.IUserService;

import lombok.extern.slf4j.Slf4j;

import java.time.Duration;

@Slf4j
@Service
public class UserServiceCache {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    public String getUserNameById(Long userId) {
        // 构建缓存键
        String cacheKey = "user:username:" + userId;

        // 从缓存中读取数据
        String username = stringRedisTemplate.opsForValue().get(cacheKey);
        if (username != null) {
            return username;
        }

        // 如果缓存中没有数据，则从数据库中查询
        username = userMapper.getUserNameById(userId);

        // 将查询结果存入缓存
        if (username != null) {
            stringRedisTemplate.opsForValue().set(cacheKey, username, Duration.ofDays(7));
        }

        return username;
    }


}
